package org.cll.lagou.study.minicat.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2020/7/2 22:39
 */
public class IRequestProcessor implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(IRequestProcessor.class);

    private Socket socket;

//    private Map<String, AbstractHttpServlet> servletMap;

    private Map<String, IHost> hostMap;

//    public IRequestProcessor(Socket socket, Map<String, AbstractHttpServlet> servletMap) {
//        this.socket = socket;
//        this.servletMap = servletMap;
//    }

    public IRequestProcessor(Socket socket, Map<String, IHost> hostMap) {
        this.socket = socket;
        this.hostMap = hostMap;
        logger.info("======>>>>>> 构造方法输出开始");
        for (Map.Entry<String, IHost> map : hostMap.entrySet()) {
            String key = map.getKey();
            logger.info("======>>> key = {}, value = {}", key, map.getValue());
            boolean keyFlag = hostMap.containsKey(key);
            if (keyFlag) {
                logger.info(" value = {}", hostMap.get(key));
            }
        }
        logger.info("======>>>>>> 构造方法输出结束");
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            IRequest iRequest = new IRequest(inputStream);
            IResponse iResponse = new IResponse(socket.getOutputStream());
// minicat 7.0以及之前使用
//            AbstractHttpServlet httpServlet = servletMap.get(iRequest.getUrl());
            // minicat 8.0使用
//            AbstractHttpServlet httpServlet = getServletFormHostMap(iRequest);
            String requestHost = iRequest.getHost();
            boolean hostFlag = hostMap.containsKey(requestHost);
            if (!hostFlag) {
                logger.error("======>>> 不包含host = {}", requestHost);
                return ;
            }
            IHost iHost = hostMap.get(requestHost);
            if (null == iHost) {
                logger.error("======>>> 不包含host = {}", requestHost);
                return ;
            }
            String url = iRequest.getUrl();
            String[] split = url.split("/");
            String contextName = split[1];
            logger.info("====== host = {}", iHost.getName());
            boolean contextFlag = iHost.getContextMap().containsKey(contextName);
            if (!contextFlag) {
                logger.error("======>>> 不包含contextName = {}", contextName);
                return ;
            }
            IContext iContext = iHost.getContextMap().get(contextName);
            String urlPattern = url.substring(contextName.length() + 1);
            logger.info("====== host = {}, context = {}", iHost.getName(), iContext.getName());
            Map<String, IMapper> mapperMap = iContext.getMapperMap();
            boolean mapperFlag = mapperMap.containsKey(urlPattern);
            if (!mapperFlag) {
                logger.error("======>>> 不包含urlPattern = {}", urlPattern);
                return ;
            }
            IMapper iMapper = mapperMap.get(urlPattern);
            logger.info("====== host = {}, context = {}, mapper = {}", iHost.getName(), iContext.getName(), iMapper.getUrl());
            AbstractHttpServlet httpServlet = iMapper.getServlet();
            if (null == httpServlet) {
                iResponse.outputHtml(iRequest.getUrl());
            } else {
                httpServlet.service(iRequest, iResponse);
            }
            socket.close();
        } catch (Exception e) {
            logger.error("****** 执行业务逻辑抛出异常" + e.getMessage(), e);
        }
    }

    private AbstractHttpServlet getServletFormHostMap(IRequest iRequest){
        if (null == iRequest || null == iRequest.getUrl() || null == iRequest.getHost()) {
            return null;
        }
        String requestHost = iRequest.getHost();
        boolean hostFlag = hostMap.containsKey(requestHost);
        if (!hostFlag) {
            return null;
        }
        IHost iHost = hostMap.get(requestHost);
        if (null == iHost) {
            return null;
        }
        String url = iRequest.getUrl();
        String[] split = url.split("/");
        String contextName = split[0];
        logger.info("====== host = {}", iHost.getName());
        boolean contextFlag = iHost.getContextMap().containsKey(contextName);
        if (!contextFlag) {
            return null;
        }
        IContext iContext = iHost.getContextMap().get(contextName);
        String urlPattern = url.substring(contextName.length() + 1);
        logger.info("====== host = {}, context = {}", iHost.getName(), iContext.getName());
        Map<String, IMapper> mapperMap = iContext.getMapperMap();
        boolean mapperFlag = mapperMap.containsKey(urlPattern);
        if (!mapperFlag) {
            return null;
        }
        IMapper iMapper = mapperMap.get(urlPattern);
        logger.info("====== host = {}, context = {}, mapper = {}", iHost.getName(), iContext.getName(), iMapper.getUrl());
        return iMapper.getServlet();
    }
}
